<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加转移记录</title>
    <link rel="stylesheet" href="./lib/layui-v2.6.3/css/layui.css" media="all">
</head>
<body>
<div class="layui-container" style="padding: 20px;">
    <form class="layui-form" id="addForm">

        <div class="layui-form-item">
            <label class="layui-form-label">转移前库位</label>
            <div class="layui-input-block">
                <select name="locationPreId" lay-verify="required" id="locationPreId" lay-filter="locationPreId">
                    <option value="">请选择库位</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">转移后库位</label>
            <div class="layui-input-block">
                <select name="locationAfterId" lay-verify="required" id="locationAfterId">
                    <option value="">请选择库位</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">选择物料</label>
            <div class="layui-input-block">
                <select name="materialId" lay-verify="required" id="materialId">
                    <option value="">请选择物料</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">数量</label>
            <div class="layui-input-block">
                <input type="number" name="quantity" step="0.01" required lay-verify="required"
                       placeholder="请输入数量" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<script src="./lib/layui-v2.6.3/layui.js"></script>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;

        // 加载库位数据
        function loadLocations() {
            $.get('/transfer/getTransferAddLocation', function (res) {
                if (res.code === 0 && res.data) {
                    var options = '';
                    res.data.forEach(function (loc) {
                        options += `<option value="${loc.storageLocationId}">${loc.warehouseName}-${loc.storageLocationName}</option>`;
                    });
                    $('#locationPreId').html(options);
                    $('#locationAfterId').html(options);
                    form.render('select');
                    // 默认加载一次物料
                    var defaultLocId = $('#locationPreId').val();
                    if (defaultLocId) loadMaterials(defaultLocId);
                } else {
                    layer.msg('加载库位失败');
                }
            });
        }

        // 根据库位ID加载物料
        function loadMaterials(locationPreId) {
            $.get('/material/getMaterialByInventory', { storageLocationId: locationPreId }, function (res) {
                if (res.code === 0 && res.data) {
                    var options = '<option value="">请选择物料</option>';
                    res.data.forEach(function (mat) {
                        options += `<option value="${mat.materialId}">${mat.materialName}</option>`;
                    });
                    $('#materialId').html(options);
                    form.render('select');
                } else {
                    $('#materialId').html('<option value="">无可用物料</option>');
                    form.render('select');
                }
            });
        }

        // 初始化加载
        $(function () {
            loadLocations();
        });

        // 监听转移前库位变化
        form.on('select(locationPreId)', function (data) {
            var selectedLocId = data.value;
            console.log('selectedLocId:', selectedLocId)
            if (selectedLocId) {
                loadMaterials(selectedLocId);
            } else {
                $('#materialId').html('<option value="">请选择库位</option>');
                form.render('select');
            }
        });

        // 监听提交事件
        form.on('submit(formDemo)', function (data) {
            $.ajax({
                url: '/transfer/add',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data.field),
                success: function (res) {
                    if (res.code === 0) {
                        layer.msg('添加成功');
                        parent.location.reload(); // 刷新父页面
                    } else {
                        layer.msg('添加失败：' + res.msg);
                    }
                },
                error: function () {
                    layer.msg('网络异常，请稍后再试');
                }
            });
            return false; // 阻止表单跳转
        });
    });
</script>
</body>
</html>
